/**
 * Created by mooshroom on 2017/7/12.
 */
avalon.ready(function () {
    var vm = avalon.define({
        $id: "index",
        ready: function () {
            avalon.scan()
            vm.buildList()

        },


        num: 100,
        list: [],
        buildList: function () {
            vm.list = avalon.range(vm.num).map(function (p1, p2, p3) {
                return {h:100}
            })
        },

        Interval: "",
        start: function () {
            vm.Interval = setInterval(vm.shareMoney, 10)
        },
        stop:function () {
            clearInterval(vm.Interval)
        },
        shareMoney: function () {
            var rich = []
            var maxMoney = 0
            vm.list.forEach(function (el, i, p3) {
                if(el.h>0){
                    el.h=el.h-1

                    share(i)
                }
            })

            function share(i) {
                var getter = GetRandomNum(0, 99)
                checkGetter()
                function checkGetter() {
                    if (getter === i) {
                        getter = GetRandomNum(0, 99)
                        checkGetter()
                    }
                }
                vm.list[getter].h=Number( vm.list[getter].h)+1

            }


        }


    })

    vm.ready()
    window.index=vm
})

function GetRandomNum(Min, Max) {
    var Range = Max - Min;
    var Rand = Math.random();
    return (Min + Math.round(Rand * Range));
}